package day06.Test1;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

interface Calculator {
    public double calculator(double a, double b);
}

class Add implements Calculator {

    @Override
    public double calculator(double a, double b) {
        return (a + b);
    }
}

class Sub implements Calculator {

    @Override
    public double calculator(double a, double b) {
        return (a - b);
    }
}

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Scanner res = new Scanner(System.in);
        System.out.println("请输入你想要进行的计算（Add/Sub）：");
        String calculator = res.next();
        String currentClassName = Test.class.getName();
        String packageName = currentClassName.substring(0, currentClassName.lastIndexOf("."));
        Class<?> clazz = Class.forName(packageName + "." + calculator);
        Object obj = clazz.newInstance();
        Method method = clazz.getMethod("calculator", double.class, double.class);
        double a = res.nextDouble();
        double b = res.nextDouble();
        double c = (double) method.invoke(obj, a, b);
        System.out.println(a + " " + calculator + " " + b + " = " + c);
    }
}
